package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.media.image.PixelMap;

import com.isotne.glidelibrary.cache.util.ImageUtils;
import com.xuexiang.xui_lib.component.dialog.config.ConfigBean;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.XToastUtils;

import java.io.File;

public class QrCodeAbilitySlice extends AbilitySlice {
    ConfigBean configBean = null;
    Image ivQqGroup;
    Image ivWinXin;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_qr_code);

        ivQqGroup = (Image) findComponentById(ResourceTable.Id_qr_code_img_qq);
        ivWinXin = (Image) findComponentById(ResourceTable.Id_qr_code_img_winxin);
        ivQqGroup.setTag("qq_group");
        ivWinXin.setTag("wei_xin_subscription_number");
        ivQqGroup.setClickedListener(new Component.ClickedListener() {

            @Override
            public void onClick(Component component) {

            }
        });
        ivWinXin.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

            }
        });

        ivQqGroup.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                showBottomSheetList(component, ((Image) component).getPixelMap());
            }
        });
        ivWinXin.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                showBottomSheetList(component, ((Image) component).getPixelMap());
            }
        });

        findComponentById(ResourceTable.Id_iv_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });

        ((Text) findComponentById(ResourceTable.Id_tv_title)).setText("扫码关注");
    }

    private String getImgFilePath(Component v) {
        File file = getContext().getExternalCacheDir();
        return file != null ? file.getPath() : getContext().getFilesDir().getPath() + File.separator + v.getTag() + ".png";
    }

    private void showBottomSheetList(Component v, final PixelMap bitmap) {
        final String imgPath = getImgFilePath(v);
        String[] words = {"发送给朋友", "保存图片", "识别图中的二维码"};


        CommonDialog commonDialog = new CommonDialog(getContext());
        commonDialog.setAutoClosable(true);
        DirectionalLayout directionalLayout = new DirectionalLayout(getContext());
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
        layoutConfig.height = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
        layoutConfig.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        directionalLayout.setOrientation(DirectionalLayout.VERTICAL);
        directionalLayout.setLayoutConfig(layoutConfig);

        boolean result = checkFile(imgPath, bitmap);
        for (int i = 0; i < words.length; i++) {
            Text text = new Text(getContext());
            text.setTag(i);
            text.setText(words[i]);
            text.setTextSize(AttrHelper.fp2px(14,getContext()));
            text.setTextAlignment(TextAlignment.VERTICAL_CENTER);
            text.setPadding(20,20,20,20);
            text.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if ((int)component.getTag() == 0) {
                        XToastUtils.toast("暂不支持分享功能!");
                    } else if ((int)component.getTag() == 1) {
                        if (result) {
                            XToastUtils.toast("图片保存成功:" + imgPath);
                        } else {
                            XToastUtils.toast("图片保存失败!");
                        }
                    } else {
                        XToastUtils.toast("暂不支识别功能!");
                    }
                }
            });
            directionalLayout.addComponent(text);
        }

        commonDialog.setOffset(0,AttrHelper.vp2px(45,getContext()));
        commonDialog.setContentCustomComponent(directionalLayout);
        commonDialog.setAlignment(LayoutAlignment.BOTTOM);
        commonDialog.show();

    }

    private boolean checkFile(String imgPath, PixelMap bitmap) {
        boolean result = isFileExists(imgPath);
        if (!result) {
            ImageUtils.savePixelMapToImage(this, bitmap, imgPath);
        }
        return result;
    }


    /**
     * 判断文件是否存在
     *
     * @param filePath 文件路径
     * @return {@code true}: 存在<br>{@code false}: 不存在
     */
    public static boolean isFileExists(final String filePath) {
        File file = getFileByPath(filePath);


        if (file.exists()) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 根据文件路径获取文件
     *
     * @param filePath 文件路径
     * @return 文件
     */
    public static File getFileByPath(final String filePath) {
        return isSpace(filePath) ? null : new File(filePath);
    }

    private static boolean isSpace(final String s) {
        if (s == null) {
            return true;
        }
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 请求浏览器
     *
     * @param url
     */
//    public void goWeb(final String url) {
//        Intent intent = new Intent(getContext(), Webviewslice.class);
//        intent.putExtra(KEY_URL, url);
//        startActivity(intent);
//    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
